/*
 *	rectangles.c
 *	written by Holmes Futrell
 *	use however you want
 */

#include "SDL.h"
#include <time.h>

#include "UGraphic.h"
#include "UTime.h"
#include "UThemes.h"
#include "UIni.h"
#include "UPLatformIOS.h"
#include "UFilesystem.h"
#include "UPathUtils.h"

#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 480

int
randomInt(int min, int max)
{
    return min + rand() % (max - min + 1);
}

void
render(SDL_Renderer *renderer)
{

}

void MainLoop()
{
    const int MAX_FPS = 100;
    
    Uint32 Delay;
    Uint32 TicksCurrent;
    Uint32 TicksBeforeFrame;
    bool Done;
    
    Done = false;
    
    CountSkipTime(); // JB - for some reason this seems to be needed when we use the SDL Timer functions.
    do {
        TicksBeforeFrame = SDL_GetTicks();
        
        // TODO: joypad
        
        // TODO: keyboard events. (change to IOS event)
        
        // TODO: display logic
        
        SwapBuffers();
        
        // FPS limiter
        TicksCurrent = SDL_GetTicks();
        Delay = 1000.0f / MAX_FPS - (TicksCurrent - TicksBeforeFrame);
        
        if (Delay > 1)
            SDL_Delay(Delay); // dynamic, maximum is 100 fps
        
        CountSkipTime();
        
    } while (!Done);
}

int
main(int argc, char *argv[])
{
    // have a lot of TODO things here
    //SDL_Renderer *renderer;
    int done;
    SDL_Event event;

    /* initialize SDL */
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
        printf("Could not initialize SDL\n");
        return 1;
    }
    
    // Platform
    Platform = new TPlatformIOS();
    
    // Initialize Path
    InitializePaths();
    
    // FileSystem
    FileSystem = new TFilesystem();
    
    // Ini + Paths
    Ini = new TIni();
    Ini->Load();
    
    // Theme
    Theme = new TTheme();
    
    // Theme
    Theme->LoadTheme(0, 0);

    /* seed random number generator */
    srand(time(NULL));
    Initialize3D();

    MainLoop();
    
    /* shutdown SDL */
    
    // release
    delete Theme; Theme = NULL;
    
    delete FileSystem; FileSystem = NULL;
    
    delete Platform; Platform = NULL;
    
    SDL_Quit();

    return 0;
}
